package kgame5.pbe.animation {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.geom.Matrix;

	/**
	 *
	 * @author Colbycheeze
	 * @description Internal class to be used by the AnimationPlayer.
	 *
	 */
	internal class Frame
	{
		private static var nextID:int;
		private var _debugID:int;

		public var numFrames:int;
		public var bitmapData:Array;
		public var width:Array;
		public var height:Array;
		public var offsetX:Array;
		public var offsetY:Array;
		public var frameID:String;

		public function toString():String
		{
			return "<Frame (" + _debugID + ") bitmapData:" + bitmapData.length + " numFrames:" + numFrames + " frameID:" + frameID + " offsetX:" + offsetX + " offsetY:" + offsetY + ">";
		}

		public function Frame(numFrames:int, bitmapData:BitmapData, numRotations:int, frameID:String, offsetX:Number=0, offsetY:Number=0)
		{
			this._debugID=nextID++;
			this.numFrames=numFrames;
			this.frameID=frameID;
			this.bitmapData=[];
			this.width=[];
			this.height=[];

			this.offsetX=[];
			this.offsetY=[];

			if (numRotations == 1)
			{
				this.createFrameWithNoRotation(bitmapData, offsetX, offsetY);
			}
			else
			{
				this.createRotatedFrame(bitmapData, numRotations, offsetX, offsetY);
			}
		}

		private function createFrameWithNoRotation(bitmapData:BitmapData, offsetX:Number=0, offsetY:Number=0):void
		{
			this.bitmapData.push(bitmapData);
			this.width.push(bitmapData.width);
			this.height.push(bitmapData.height);
			this.offsetX.push(-offsetX);
			this.offsetY.push(-offsetY);
		}

		private function createRotatedFrame(bitmapData:BitmapData, numRotations:int, offsetX:Number=0, offsetY:Number=0):void
		{
			// In order to determine how large the bitmap must be in order to rotate the image, we must
			// find the largest side of the X and Y. We then find the amount of offset to place the object in
			// so that it is centered within the bitmap, that way it can rotate w/o being cut off at any point
			var largeSideX:int=Math.max(Math.abs(offsetX), offsetX + bitmapData.width);
			var largeSideY:int=Math.max(Math.abs(offsetY), offsetY + bitmapData.height);

			var tx:Number=(largeSideX) * Math.SQRT2;
			var ty:Number=(largeSideY) * Math.SQRT2;

			var maxTxy:Number=Math.max(tx, ty);

			var bdSize:Number=Math.ceil(Math.max(largeSideX * 2, largeSideY * 2) * Math.SQRT2);
			var mat:Matrix=new Matrix(1, 0, 0, 1, maxTxy, maxTxy);
			var bd:BitmapData=new BitmapData(bdSize, bdSize, true, 0);

			var container:Sprite=new Sprite();
			var bmpHolder:Sprite=new Sprite();
			var bmpData:BitmapData=new BitmapData(largeSideX, largeSideY, true, 0);
			var bmp:Bitmap=new Bitmap(bitmapData, PixelSnapping.ALWAYS, true);
			bmp.x=offsetX;
			bmp.y=offsetY;
			bmpHolder.addChild(bmp);
			container.addChild(bmpHolder);

			var ri:int=0;
			while (ri < numRotations)
			{
				bd.fillRect(bd.rect, 0x0);
				bd.draw(container, mat);

				this.bitmapData[ri]=bd.clone();
				this.width[ri]=bmpHolder.width;
				this.height[ri]=bmpHolder.height;

				this.offsetX[ri]=maxTxy;
				this.offsetY[ri]=maxTxy;

				bmpHolder.rotation+=360 / numRotations;

				ri++;
			}
		}
	}
}


